import { defineStore } from 'pinia';

export const useUserStore = defineStore('user', {
  state: () => ({
    token: uni.getStorageSync('token') || '',
    userInfo: uni.getStorageSync('userInfo') || {},
    merchantId: uni.getStorageSync('merchantId') || '',
    messageData: {}, // 消息列表

    loginRedirectUrl: '', // 从其他页面登录重定向地址
  }),

  getters: {
    isLogin: (state) => !!state.token, // 判断是否登录
  },

  actions: {
    setToken(token: string) {
      this.token = token;
      uni.setStorageSync('token', token);
    },

    setMerchantId(id) {
      this.merchantId = id;
      uni.setStorageSync('merchantId', id);
    },

    setUserInfo(userInfo: any) {
      this.userInfo = userInfo;
      uni.setStorageSync('userInfo', userInfo);
    },
    logout() {
      this.token = '';
      this.userInfo = {};
      this.messageData = {};
      this.merchantId = '';
      uni.removeStorageSync('token');
      uni.removeStorageSync('userInfo');
      uni.removeStorageSync('merchantId');
    },
  },
});
